#include "PlatformProperty.h"

static bool isMoveLimited(int32_t pos) {
    int32_t min = lcfg::LocalConfig::getInstance().getInt<int32_t>(lcfg::LCFG_Min_Backrest_angle);
    int32_t max = lcfg::LocalConfig::getInstance().getInt<int32_t>(lcfg::LCFG_Max_Backrest_angle);
    if (pos < min || pos > max) {
        VHALOG.d("SEAT_BACKREST_ANGLE_1_MOVE isMoveLimited pos=%d, min=%d, max=%d", pos, min, max);
        return true;
    }
    return false;
}

//  N5  [SW_FC_004_050] 3.2.19 第二排靠背调节 2ndRowSeatBackrestAdjustment--位置
SYSTEM_READ_PROPERTY(SEAT_BACKREST_ANGLE_2_POS, int32_t) {
    builder.area(VehicleAreaSeat::ROW_2_LEFT)
        .initValue([]() { return 0; })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                VHALOG.d("SEAT_BACKREST_ANGLE_2_POS drvr cc474[%d], cc551[%d]",
                         SIGNAL.getcarconfig474(), SIGNAL.getcarconfig551());
                if (SIGNAL.carConfigIs(SignalRX::carconfig474,
                                       sdb::CarConfig474::_03,
                                       sdb::CarConfig474::_04,
                                       sdb::CarConfig474::_05,
                                       sdb::CarConfig474::_81) &&
                    SIGNAL.carConfigIs(SignalRX::carconfig551,
                                       sdb::CarConfig551::_02)) {
                    VHALOG.d("SEAT_BACKREST_ANGLE_2_POS drvr Active");
                    return PropertyStatus::Active;
                }
                VHALOG.d("SEAT_BACKREST_ANGLE_2_POS drvr NotAvailable");
                return PropertyStatus::NotAvailable;
            },
            SignalRX::carconfig474,
            SignalRX::carconfig551)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t& last) {
                auto angle = SIGNAL.getRowSecLeInclPosAngle();
                VHALOG.d("SEAT_BACKREST_ANGLE_2_POS drvr RowSecLeInclPosAngle %d", angle);
                if (isMoveLimited(angle)) {
                    return last;
                }
                return angle;
            },
            SignalRX::RowSecLeInclPosAngle)
        .area(VehicleAreaSeat::ROW_2_RIGHT)
        .initValue([]() { return 0; })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                VHALOG.d(
                    "SEAT_BACKREST_ANGLE_2_POS pass cc474[%d], cc551[%d]",
                    SIGNAL.getcarconfig474(), SIGNAL.getcarconfig551());
                if (SIGNAL.carConfigIs(SignalRX::carconfig474,
                                       sdb::CarConfig474::_03,
                                       sdb::CarConfig474::_04,
                                       sdb::CarConfig474::_05,
                                       sdb::CarConfig474::_81) &&
                    SIGNAL.carConfigIs(SignalRX::carconfig551,
                                       sdb::CarConfig551::_02)) {
                    VHALOG.d("SEAT_BACKREST_ANGLE_2_POS pass Active");
                    return PropertyStatus::Active;
                }
                VHALOG.d("SEAT_BACKREST_ANGLE_2_POS pass NotAvailable");
                return PropertyStatus::NotAvailable;
            },
            SignalRX::carconfig474,
            SignalRX::carconfig551)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t last) {
                auto angle = SIGNAL.getRowSecRiInclPosAngle();
                VHALOG.d("SEAT_BACKREST_ANGLE_2_POS pass RowSecRiInclPosAngle %d", angle);
                if (isMoveLimited(angle)) {
                    return last;
                }
                return angle;
            },
            SignalRX::RowSecRiInclPosAngle);
}